package demo1;

import java.lang.reflect.Array;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-03-16
 * Time: 15:23
 */

/*class MyArray {
    public Object[] objects = new Object[10];

    public Object getPos(int pos) {
        return this.objects[pos];
    }

    public void setVal(int pos, Object val) {
        objects[pos] = val;
    }
}*/

class MyArray <T> {
    //public Object[] objects = new Object[10];
    //public T[] objects = new T[10];
    public T[] objects ;
    public MyArray() {
    }

    public MyArray(Class<T> clazz, int capacity) {
        objects = (T[]) Array.newInstance(clazz, capacity);
    }

    public T getPos(int pos) {
        return objects[pos];
    }

    public void setVal(int pos,T val) {
        objects[pos] = val;
    }

    public T[] getArray() {
        return objects;
    }
}
// 工作当中,会这样写泛型
class MyArray2 <T> {
    public Object[] objects = new Object[10];
    //public T[] objects = new T[10];
    //public T[] objects ;//这样写也是不好的 ！！！


    public T getPos(int pos) {
        return (T)objects[pos];
    }

    public void setVal(int pos,T val) {
        objects[pos] = val;
    }

    public Object[] getArray() {
        return objects;
    }
}


public class Test1 {
    public static void main(String[] args) {
        MyArray2<Integer> myArray1 = new MyArray2<Integer>();
        myArray1.setVal(0,23);
        myArray1.setVal(1,10);
        int val0 = myArray1.getPos(0);
        int val1 = myArray1.getPos(1);
        System.out.println(val0);
        System.out.println(val1);
    }

    public static void main3(String[] args) {
        MyArray<Integer> myArray1 = new MyArray<Integer>(Integer.class,10);
        Integer[] integers = myArray1.getArray();
    }

    public static void main2(String[] args) {
        MyArray<Integer> myArray1 = new MyArray<Integer>();
        myArray1.setVal(0,23);
        myArray1.setVal(1,10);
        int val0 = myArray1.getPos(0);
        int val1 = myArray1.getPos(1);
        System.out.println(val0);
        System.out.println(val1);

        System.out.println("=============");
        MyArray<String> myArray2 = new MyArray<>();
        myArray2.setVal(0,"hello");
        myArray2.setVal(1,"world");
        String ret = myArray2.getPos(1);
        System.out.println(ret);

    }

    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setVal(0,"123");
        myArray.setVal(1,10);

        int val = (int)myArray.getPos(1);
        System.out.println(val);
    }

}
